package utils;

import okhttp3.Headers;
import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.nio.client.HttpAsyncClient;
import org.apache.http.util.EntityUtils;
import org.apache.kafka.common.protocol.types.Field;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ZHANGPENGYU002
 * @date 2021/9/8 11:15
 */
public class HttpUtils {
    static LoggerUtile logger =LoggerUtile.getLogger("garenaJava-app");
    /**
     * responseCode: java.net.HttpURLConnection
     * header: org.apache.http.protocol.HTTP、org.springframework.http.HttpHeaders、org.springframework.http.MediaType
     */
    public static CloseableHttpResponse post(String url,  byte[] body) throws Exception {


        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        //设置请求头
        HttpPost post = new HttpPost(url);

//        for (String key : headers.keySet()) {
//            post.setHeader(key, headers.get(key));
//        }

        post.setHeader("accept-encrypt", "");
//        post.setHeader("gh-header", "1-1-10011-2103130010-0");
        post.setHeader("gh-header", "1-1-10012-2103090109-749305350");
        post.setHeader("content-encrypt", "");
        post.setHeader("user-agent", "Dalvik/2.1.0 (Linux; U; Android 12; Pixel 4 Build/SQ3A.220605.009.A1)");
//        post.setHeader("content-type", "application/x-www-form-urlencoded");
        post.setHeader("content-type", "application/octet-stream");
        HttpEntity entity = new ByteArrayEntity(body);
        //包括但不限于以下许多种HttpEntity
        // InputStreamEntity
        // FileEntity / NFileEntity
        // GzipCompressingEntity / GzipDecompressingEntity
        post.setEntity(entity);

        CloseableHttpResponse response = httpClient.execute(post);


        return response;
    }
    public static CloseableHttpResponse post(String url,  String body) throws Exception {


        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        //设置请求头
        HttpPost post = new HttpPost(url);
        post.setHeader("accept", "application/json, text/javascript, */*; q=0.01");
        post.setHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8");


        StringEntity entity = new StringEntity(body,"UTF-8");
        post.setEntity(entity);
        CloseableHttpResponse response = httpClient.execute(post);
        return response;
    }
    public static CloseableHttpResponse post(String url, String body, Map<String,String> headers, HttpHost proxy) throws Exception {
        CloseableHttpClient httpClient;
        CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.createDefault();

        httpAsyncClient.start();
        if (proxy != null){
            logger.info("post开始启动请求proxy is:"+ proxy.getHostName()+":"+proxy.getPort());
            httpClient = HttpClientBuilder.create().setProxy(proxy).build();
        } else {
            httpClient = HttpClientBuilder.create().build();;
        }


        //设置请求头
        HttpPost post = new HttpPost(url);
        post.setHeader("accept", "application/json, text/javascript, */*; q=0.01");
        post.setHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8");
        for (String key : headers.keySet()) {
            post.setHeader(key, headers.get(key));
        }
        StringEntity entity = new StringEntity(body,"UTF-8");
        post.setEntity(entity);
        CloseableHttpResponse response = httpClient.execute(post);
        return response;
    }
    public static CloseableHttpResponse get(String url) throws Exception {

        // 创建Get请求
        //对象传参的方式，注意在传参的时候必须将参数拆开单个传
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        //设置请求头
        httpGet.setHeader("accept", "application/json, text/javascript, */*; q=0.01");
        httpGet.setHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8");

        CloseableHttpResponse response = httpClient.execute(httpGet);
        return response;
    }

}